<?php
namespace app\api\controller;
use think\Controller;
use app\api\model\UserModel;
use think\Session;
class User extends Controller
{
     function login(){
         $username = input('get.username');
         $password = input('get.password');
         $model=new  UserModel();
         $result = $model->login($username,$password);
         if ($result) {
             session_start();
             $sessionID = session_id();
             $redis = getRedis();
             $redis->set($result['userid'],$sessionID);
             return json_encode(['sessionID'=>$sessionID, 'userid'=>$result['userid']]);
         }else {
             return  json_encode(['id'=>0]);
         }
     }
     function  register(){
         $username = input('get.username');
         $password = input('get.password');
         $phone = input('get.phone');
         $gender = input('get.gender');
         $model=new  UserModel();
         $result = $model->register($username,$password,$phone,$gender);

         if ($result==1)
         {
             return  json_encode(['success'=>-1]);
         }
          else if ($result==2)
         {
             return  json_encode(['success'=>1]);
         }
          else if ($result==3)
          {
              return  json_encode(['success'=>0]);
          }
     }
    function issuepicture() {
        $userid = input('get.userid');
        $sessionid = input('get.sessionid');

        $redis = getRedis();
        if ($sessionid!='' && $sessionid == $redis->get($userid)) {

            $file = request()->file('picture1');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    $model = new UserModel();
                    $result = $model->issuepicture($userid,$info->getSaveName());
                    return json_encode($result);
                }else{
                    return json_encode(['error'=>$file->getError()]);
                }
            }
        }
       else{
           return  json_encode(['error'=>'sessionid无效']);
       }
    }
    function getpicture() {
        $userid = input('get.userid');
        $sessionid = input('get.sessionid');

        $redis = getRedis();
        if ($sessionid!='' && $sessionid == $redis->get($userid)) {
            $model = new UserModel();
            $result = $model->getpicture($userid);
            return json_encode($result);
        }
        return  json_encode(['error'=>'sessionid无效']);
    }
    function gameenroll() {
        $sessionid = input('get.sessionid');
        $userid = input('get.userid');
        $gameid = input('get.gameid');

        session_start();
        $redis = getRedis();
        if ($sessionid!='' && $sessionid == $redis->get($userid)) {
            $model = new UserModel();
            $result = $model->gameenroll($userid,$gameid);
            return json_encode($result);
        }
        return  json_encode(['error'=>'sessionid无效']);
    }
    function repassword() {
        $userid = input('get.userid');
        $sessionid = input('get.sessionid');
        $oldpassword = input('get.oldpassword');
        $newpassword = input('get.newpassword');

        $redis = getRedis();
        if ($sessionid!='' && $sessionid == $redis->get($userid)) {
            $model = new UserModel();
            $result = $model->repassword($userid,$oldpassword,$newpassword);
            return json($result);
        }
        return  json(['error'=>'sessionid无效']);
    }
    function updatenickname() {
        $userid = input('get.userid');
        $nickname = input('get.nickname');
        $sessionid = input('get.sessionid');

        $redis = getRedis();
        if ($sessionid!='' && $sessionid == $redis->get($userid)) {
            $model = new UserModel();
            $result = $model->updateNickname($userid,$nickname);
            return json($result);
        }
        return  json(['error'=>'sessionid无效']);
    }


    function updateavatar() {

        $userid = input('post.userid');
        $sessionid = input('post.sessionid');

        $redis = getRedis();
        if ($sessionid!='' && $sessionid == $redis->get($userid)) {
            $file = request()->file('image');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){ //上传成功
                    $model = new UserModel();
                    $result = $model->updateAvatar($userid,$info->getSaveName());
                    return json($result);
                }else{
                    // 上传失败获取错误信息
                    return json(['error'=>$file->getError()]);
                }
            }
        }
        return  json(['error'=>'sessionid无效']);
    }
}